<?php


namespace app\suguan\controller;


use think\Db;

class Baoxiu extends Base
{
    public function index()
    {
        $page = request()->param('page', '1', 'intval');
        $limit = request()->param('limit', 10, 'intval');
        $room_id = request()->param('room_id', '');
        $username = request()->param('username', '');
        $status = request()->param('status', '');
        $house_id = $this->house_id();
        $where[] = ['bx.house_id', '=', $house_id];
        if ($room_id) {
            $where[] = ['room_id', '=', $room_id];
        }
        if ($username) {
            $uid = model('Member')->where('house_id',$this->house_id())->where('username', 'like', '%' . $username . '%')->column('uid');
            $where[] = ['uid', 'in', $uid];
        }
        if($status != '') {
            $where[] = ['status','=', $status];
        }
        $list = Db::name('baoxiu')->alias('bx')
            ->join('room r', 'bx.room_id = r.id')
            ->where($where)->page($page, $limit)
            ->field('r.title as room,bx.id,bx.uid,bx.room_id,bx.images,bx.status,bx.content,bx.create_time,bx.house_id')
            ->order('bx.id desc')
            ->select();
        foreach ($list as $k => $v) {
            $list[$k]['username'] = model('Member')->where('uid', $v['uid'])->value('username');
            $list[$k]['people'] = model('Member')->where(['house_id' => $house_id,'sushe_id' => $v['room_id']])->column('username');//宿舍成员
            $list[$k]['image_url'] = model('File')->getPath($v['images']);
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        $total = Db::name('baoxiu')->alias('bx')
            ->join('room r', 'bx.room_id = r.id')
            ->where($where)->count();
        $room = model('Room')->where('house_id', $house_id)->column('title', 'id');
        $data = [
            'list' => $list,
            'total' => $total,
            'room' => $room
        ];
        return $this->ok($data);
    }

    public function save()
    {
        $params = request()->only(['id','room_id']);
        if(!isset($params['id'])){
            return $this->fail('参数错误');
        }
        $res = Db::name('baoxiu')->where(['house_id' => $this->house_id(),'room_id' => $params['room_id'],'id'=>$params['id']])->update(['status'=>1]);
        if($res){
            return $this->ok('操作成功');
        }else{
            return $this->fail('操作失败');
        }
    }
}